//后台添加数据
import {

  Modal,
} from "antd";
import type { LocalStorageDataType } from "@/tool/type";
import CrowdFundingForm from "@/components/CrowdFundingForm";
import dayjs from "dayjs";








type Props = {
  open: boolean;
  item: LocalStorageDataType | null;
  onSave: (item: LocalStorageDataType) => void;
  onCancel: () => void;
};

const HTModifyConfig: React.FC<Props> = ({ open, item, onSave, onCancel }) => {
  // 用 Date 对象数组作为 initialValues
  const initialValues = item
    ? {
        ...item,
        time: Array.isArray(item.time)
          ? [dayjs(item.time[0]), dayjs(item.time[1])]
          : [],
      }
    : {};

  const handleFinish = (values: LocalStorageDataType) => {
    // 确保 time 是字符串数组
    let time: string[] = [];
    if (Array.isArray(values.time)) {
      time = values.time.map((t: any) =>
        t && typeof t.toISOString === "function" ? t.toISOString() : t
      );
    } else if (typeof values.time === "string") {
      time = [values.time];
    }
    if (item) {
      onSave({ ...item, ...values, time });
    }
  };

  return (
    <Modal
      open={open}
      title="修改众筹信息"
      onCancel={onCancel}
      footer={null}
      destroyOnHidden
    >
      <CrowdFundingForm
        initialValues={initialValues}
        onFinish={handleFinish}
        onCancel={onCancel}
        submitText="变更"
      />
    </Modal>
  );
};

export default HTModifyConfig;
